package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 马祥和
 * @version 1.0
 * @function
 * @time 2025/7/31 16:13
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean page(StudentQueryParam sqp){
        PageHelper.startPage(sqp.getPage(),sqp.getPageSize());

        List<Student> studentList = studentMapper.page(sqp);

        Page<Student> page = (Page<Student>) studentList;

        return new PageBean(page.getTotal(), page.getResult());

    }

    @Override
    public void delete(List<Integer> ids) {studentMapper.delete(ids);}

    @Override
    public void insert( Student student) {

        student.setViolationCount((short)0);
        student.setViolationScore((short)0);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.insert(student);

    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void updateScore(Integer id, Integer score) {

        studentMapper.updateScore(id, score);
    }
}
